<?php
class Shoutbox {
	var $collection		= null;		
	var $cache			= null;
	var $clear			= null;	
	/*
	------- table nlogs
	------- field
	* userid
	* content
	* time
	*/
	public function __construct() {
		if(DB_MONGO_ENABLE) {
			$mongo 				= new Mongo();
			$this->collection 	= $mongo->selectDB('vfriend')->selectCollection('shoutbox');				
		}
	}
	public function delete() {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove();
		}
	}
	public function deleteByTime($intUserId) {
		if(DB_MONGO_ENABLE) {
			$intTime = time() - LOGS_DELETE_TIME; 
			$this->collection->remove(array('time'=>'$lt ' . $intTime));
		}
	}
	public function deleteById($intId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('_id' => new MongoId($intId)), true);
		}
	}
	public function insert($intUserId, $strContent) {	
		try {
			if(DB_MONGO_ENABLE) {
				$objInsert = array(	'userid'		=> (int)$intUserId,
									'content'		=> (string)$strContent,
									'time'			=> time()
							);		
				$this->collection->insert($objInsert);				
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Shoutbox::insert - ' .$e->getMessage());			
			return null;
		}
	}
	public function getShoutbox($intOffset, $intLimit, &$intTotal=0) {
		try {			
			if(DB_MONGO_ENABLE) {				
				$objSort	= array('time'=>-1);
				$objCursor	= $this->collection->find();
				$arrData 	= $objCursor->sort($objSort)->limit((int)$intLimit)->skip((int)$intOffset);				
				$intTotal	= $objCursor->count();				
				if((object)$arrData) {
					$arrResult = null;
					$intId	   = $intLimit;
					foreach($arrData as $value) {
						$value['postdate'] 	= date('Y-m-d H:i:s',$value['time']);
						$arrResult[$intId] 	= $value;
						$intId--;
					}
					ksort($arrResult);
					return $arrResult;
				}
			}
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Shoutbox::getShoutbox - ' .$e->getMessage());			
			return null;
		}
	}
}
?>